//index.js
//获取应用实例../..
const app = getApp()
var that;
Page({
  data: {
    canIUseUserInfo: wx.canIUse('button.open-type.getUserInfo'),
    canIUseLocation: false,
    getUserInfoAuthFlg: false
  },
  onLoad:function(){
      console.log("globalData=", app.globalData);
      var that = this;
      wx.getSetting({
        success(res){
          console.log(res)
          var flg = true;
          if (res.authSetting['scope.userLocation'])  {  
            that.setData({
              canIUseLocation: true
            });                   
          }else {
            flg =false;
          }
          if (res.authSetting['scope.userInfo']) {
            that.setData({
              getUserInfoAuthFlg:true
            })
          }else {
            // flg = false;
          }
          if(flg) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
  },
  bindGetUserInfo: function (e) {
    var that = this;
    console.log('用户授权结果',e)
    if (e.detail.userInfo) {
      that.setData({
        canIUseUserInfo: true,
        getUserInfoAuthFlg:true
      })
      app.globalData.userInfo.avatarUrl = e.detail.avatarUrl;
      app.globalData.userInfo.nickName = e.detail.nickName;
      if (that.data.canIUseLocation) {
        wx.navigateBack({
          delta:1
        })
      }else {
        wx.showToast({
          title: '请授权位置',
          image: '/images/gantan.png'
        })
      }
    } else {
      wx.showToast({
        title: '您拒绝了授权',
        image: '/images/gantan.png'
      })
    }
  },
  openLocationSet:function() {
    var that = this;
    wx.openSetting({
      success(res) {
        console.log('用户授权结果',res.authSetting)
        that.setData({
          // getUserInfoAuthFlg: res.authSetting['scope.userInfo'],
          canIUseLocation: res.authSetting['scope.userLocation']
        })
      }
    })        
  },
  getLocation:function() {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        app.globalData.locationAuthFlg = true;
        console.log('获取经纬度成功', res)
        app.globalData.header.lat = parseFloat(res.latitude)
        app.globalData.header.lng = parseFloat(res.longitude)
        that.setData({
          canIUseLocation: true
        })
        console.log('------->>', that.data.getUserInfoAuthFlg, that.data.canIUseLocation)
        // if (that.data.getUserInfoAuthFlg && !that.data.canIUseLocation) {
        if (that.data.canIUseLocation) {
          wx.navigateBack({
            delta:1
          })
        }
      },
      fail: function (res) {
        console.log('获取经纬度fail', res);
        wx.showToast({
          title: '定位失败',
          image:'/images/gantan.png'
        })
      }
    });
  },
  onShow: function () {
    
    this.getLocation();
    
  },

})